package mgodb

import (
	"fmt"
	"github.com/facebookgo/inject"
	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"github.com/pkg/errors"
	"jgt-oper/src/model"
	"jgt-oper/src/schema"
	"jgt-oper/src/service/mongo"
	"time"
)

//	cEnterpriseVisit 企业来访
const cEnterpriseVisit = "oper_enterprise_visit"

// EVisit 企业来访
type EVisit struct {
	DB *mongo.DB
}

// Init 初始化
func (a *EVisit) Init(g *inject.Graph, db *mongo.DB) {
	a.DB = db

	// 初始化mongo索引
	a.DB.C(cEnterpriseVisit, func(c *mgo.Collection) error {
		c.EnsureIndexKey("record_id")
		return nil
	})

	var val model.IEVisit = a
	g.Provide(&inject.Object{Value: val, Name: "IEVisit"})
}

// QVisitPage 列表查询
func (a *EVisit) QVisitPage(params *schema.EVQParams, currentPage, pageSize uint) (int, []*schema.EnterpriseVisit, error) {
	query := bson.M{
		"personnel_name":         bson.M{"$regex": params.PersonnelName},
		"contract_tel":           bson.M{"$regex": params.ContractTel},
		"from_enterprise":        bson.M{"$regex": params.FromEnterprise},
		"visiting_enterprise_id": bson.M{"$regex": params.VisitingEnterpriseID},
		"main_content":           bson.M{"$regex": params.MainContent},
	}
	if params.PersonnelTotal != 0 {
		query["personnel_total"] = params.PersonnelTotal
	}
	if params.StartTime != 0 {
		query["start_time"] = bson.M{"$gte": params.StartTime}
	}
	if params.StartTime != 0 {
		query["end_time"] = bson.M{"$lte": params.EndTime}
	}
	total := 0
	var err error
	items := make([]*schema.EnterpriseVisit, 0)
	err = a.DB.C(cEnterpriseVisit, func(c *mgo.Collection) error {
		total, err = c.Find(query).Count()
		if err != nil {
			return err
		}

		return c.Find(query).Sort("-created").Skip(int((currentPage - 1) * pageSize)).Limit(int(pageSize)).All(&items)
	})
	if err != nil {
		return 0, nil, errors.Wrapf(err, "查询分页数据发生错误")
	}

	return total, items, nil
}

// Cancel 作废取消
func (a *EVisit) Cancel(recordID string) error {
	err := a.DB.C(cEnterpriseVisit, func(c *mgo.Collection) error {
		return c.Update(bson.M{"record_id": recordID, "status": 1}, bson.M{"$set": bson.M{"status": 2}})
	})
	if err != nil {
		return errors.Wrapf(err, "更新数据发生错误")
	}
	return nil
}

// Create 创建来访
func (a *EVisit) Create(item *schema.EnterpriseVisit) error {
	err := a.DB.C(cEnterpriseVisit, func(c *mgo.Collection) error {
		id, err := a.DB.IncrID(cEnterpriseVisit)
		if err != nil {
			return err
		}
		item.ID = id
		return c.Insert(item)
	})
	if err != nil {
		return errors.Wrapf(err, "创建企业来访发生错误")
	}

	return nil
}

// Get 查询
func (a *EVisit) Get(recordID string) (*schema.EnterpriseVisit, error) {
	item := new(schema.EnterpriseVisit)
	err := a.DB.C(cEnterpriseVisit, func(c *mgo.Collection) error {
		return c.Find(bson.M{"record_id": recordID}).One(item)
	})
	if err != nil {
		return nil, err
	}
	return item, nil
}

// QueryPage 移动端查询分页
func (a *EVisit) QueryPage(params *schema.ApiEVQParams) (int, []*schema.EnterpriseVisit, error) {
	query := bson.M{
		"personnel_name":         bson.M{"$regex": params.PersonnelName},
		"visiting_enterprise_id": params.EnterpriseID,
		"is_come":                params.IsCome,
		"deleted":                0,
	}
	if v := params.LastID; v > 0 {
		query["_id"] = bson.M{"$lt": v}
	}
	fmt.Println("--->", query)
	total := 0
	var err error
	items := make([]*schema.EnterpriseVisit, 0)
	err = a.DB.C(cEnterpriseVisit, func(c *mgo.Collection) error {
		n, err := c.Find(query).Count()
		if err != nil {
			return err
		}
		total = n
		if total == 0 {
			return nil
		}

		q := c.Find(query).Sort("-_id")
		q = q.Limit(params.PageSize)

		return q.All(&items)
	})
	if err != nil {
		return 0, nil, err
	}

	return total, items, nil
}

func (a *EVisit) Resend(recordID string) error {
	err := a.DB.C(cEnterpriseVisit, func(c *mgo.Collection) error {
		return c.Update(bson.M{"record_id": recordID, "deleted": 0}, bson.M{"$inc": bson.M{"send_times": 1}})
	})
	if err != nil {
		return errors.Wrapf(err, "更新数据发生错误")
	}
	return nil
}
